import { getAction, postAction, putAction, deleteAction } from '@/api/manage'
import { getToken } from '@/utils/auth'

const state = {
  token: getToken()
}

const actions = {

  // POST
  postAction({ commit, params }) {
    return new Promise((resolve, reject) => {
      postAction(params.url, params.params).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // GET
  getAction({ commit }, params) {
    return new Promise((resolve, reject) => {
      getAction(params.url, params.params).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // PUT
  putAction({ commit }, params) {
    return new Promise((resolve, reject) => {
      putAction(params.url, params.params).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // DELETE
  deleteAction({ commit }, params) {
    return new Promise((resolve, reject) => {
      deleteAction(params.url, params.params).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  actions
}
